package com.example.reagent_management.service.works

import android.graphics.Point
import android.view.WindowManager
import androidx.annotation.NonNull
import androidx.annotation.RestrictTo
import androidx.lifecycle.LifecycleCoroutineScope
import androidx.lifecycle.LifecycleOwner
import androidx.work.Data
import androidx.work.WorkManager
import com.example.lib_base.event.ShareViewModel
import com.example.lib_base.utils.MMKVUtils
import com.example.reagent_management.service.works.HistoryWork.Companion.HISTORY_INFO_BOOLEAN
import com.example.reagent_management.service.works.HistoryWork.Companion.HISTORY_INFO_LONG
import com.example.reagent_management.service.works.HistoryWork.Companion.HISTORY_INFO_STRING
import com.example.reagent_management.service.works.HistoryWork.Companion.HISTORY_UPDATE
import com.example.reagent_management.service.works.MessageWork.Companion.MESSAGE_MESSAGES
import com.example.reagent_management.service.works.MessageWork.Companion.MESSAGE_REMIND_DATE
import com.example.reagent_management.service.works.MessageWork.Companion.MESSAGE_REMIND_ID
import com.example.reagent_management.service.works.MessageWork.Companion.MESSAGE_TIME
import com.example.reagent_management.service.works.MessageWork.Companion.MESSAGE_TYPE
import com.example.reagent_management.service.works.MessageWork.Companion.MESSAGE_WORK_RESULT
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.util.*

//传入后台插入数据库所需的   记录信息  历史操作信息
fun setHistoryData(arrayString: Array<String>, arrayLong: LongArray, operate: Boolean,update:Boolean = false): Data {
    return Data.Builder().putStringArray(HISTORY_INFO_STRING, arrayString)
        .putLongArray(HISTORY_INFO_LONG, arrayLong).putBoolean(HISTORY_INFO_BOOLEAN, operate)
        .putBoolean(HISTORY_UPDATE,update)
        .build()
}

//传入消息
fun setMessageData(message: String, type: Int,remindId:Long = 0L,time:Long = System.currentTimeMillis()): Data {
    return Data.Builder().putString(MESSAGE_MESSAGES, message)
        .putInt(MESSAGE_TYPE, type)
        .putLong(MESSAGE_REMIND_ID,remindId)
        .putLong(MESSAGE_TIME,time)
        .build()
}

//workRemind Helper 立刻工作提醒 温湿度提醒 库存提醒
fun setWorkRemind(
    owner: LifecycleOwner,
    shareEvent: ShareViewModel,
    workManager: WorkManager,
    workResult: () -> UUID,
    scope:LifecycleCoroutineScope
) {
    scope.launch {
        workManager.getWorkInfoByIdLiveData(workResult()).observe(owner, {
            if (it != null) {
                val result = it.outputData.getLong(MESSAGE_WORK_RESULT, -1L)
                val messages = MMKVUtils.getHelper().decodeLong(MESSAGE_REMIND_DATE, 0L)
                if (result != -1L) {
                    val reminds = shareEvent.messageRemind.value ?: 0
                    shareEvent.messageRemind.value = reminds.plus(1).plus(messages)
                }
            }
        })
    }
}

//延期工作提醒 用于日期提醒
fun setWorkRemind(
    owner: LifecycleOwner,
    workManager: WorkManager,
    workResult: () -> UUID,
    scope:LifecycleCoroutineScope
){
    scope.launch {
        val woResult = workResult()
        workManager.getWorkInfoByIdLiveData(woResult).observe(owner, {
            if (it != null) {
                val result = it.outputData.getLong(MESSAGE_REMIND_DATE, -1L)
                val messages = MMKVUtils.getHelper().decodeLong(MESSAGE_REMIND_DATE,0L)
                if (result != -1L) {
                    MMKVUtils.getHelper().encode(MESSAGE_REMIND_DATE,messages+1)
                }
            }
        })
    }
}



